// import store from 'store'
import JSEncrypt from 'jsencrypt'
import { getPublicKey, setPublicKey } from '@/utils/publicKey'
import { getPublicKey as getUserPublicKey } from '@/api/user'

export default async function encrypt(value) {
  let key = getPublicKey()
  try {
    if (!key || key === 'undefined') {
      const result = await getUserPublicKey()
      key = result.data.pubKey
      key = key.replace(/\. +/g, '')
      key = key.replace(/[\r\n]/g, '')
      setPublicKey(key)
    }
    const encrypt = new JSEncrypt()
    encrypt.setPublicKey(key)
    return encrypt.encrypt(value)
  } catch (err) {
    console.log(err)
  }
}
